package cn.wolfcode.luowowo2.mgrsite.web.controller;

import cn.wolfcode.luowowo2.article.domain.Destination;
import cn.wolfcode.luowowo2.article.domain.Region;
import cn.wolfcode.luowowo2.article.service.IDestinationService;
import cn.wolfcode.luowowo2.article.service.IRegionService;
import cn.wolfcode.luowowo2.common.query.QueryObject;
import cn.wolfcode.luowowo2.common.util.AjaxResult;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Albert on 2019/7/8.
 */
@Controller
@RequestMapping("region")
public class RegionController {
    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo pageInfo = regionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "region/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Region region){
        AjaxResult ajaxResult = new AjaxResult();
        regionService.saveOrUpdate(region);
        return ajaxResult;
    }

    @RequestMapping("changeHotValue")
    @ResponseBody
    public AjaxResult changeHotValue(Boolean hot,Long id){
        AjaxResult ajaxResult = new AjaxResult();
        regionService.changeHotValue(hot,id);
        return ajaxResult;
    }

    @RequestMapping("getDestByDeep")
    @ResponseBody
    public List<Destination> getDestByDeep(Integer deep){
        AjaxResult ajaxResult = new AjaxResult();
        List<Destination> list = destinationService.getDestByDeep(deep);
        return list;
    }

    @RequestMapping("delete")
    @ResponseBody
    public AjaxResult deleteById(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        //regionService.deleteById(id); //暂时不要使用,后续再用
        return ajaxResult;
    }

    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public List<Destination> getDestByRegionId(Long rid){
        AjaxResult ajaxResult = new AjaxResult();
        Region region = regionService.get(rid);

        List<Destination> list = destinationService.getDestByIds( region.getRefIds());
        return list;
    }
}
